package ribbtimq_1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

//ribbitmq消费者接受消息
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //建立连接
        ConnectionFactory c = new ConnectionFactory();
        c.setHost("192.168.64.140");
        c.setPort(5672);
        c.setUsername("admin");
        c.setPassword("admin");
        Connection ch = c.newConnection();
        Channel cha = ch.createChannel();
        //定义队列(谁先启动谁来创建队列)
        cha.queueDeclare("helloWord", false, false, false, null);
        //创建回调对象
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] bytes = delivery.getBody();
                String msg = new String(bytes);
                System.out.println("以收到消息" + msg);
            }
        };
        //取消回调对象
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };
        //消费接受消息
        cha.basicConsume("helloWord", true, deliverCallback, cancelCallback);
    }
}
